package com.pengxun.common.config.mybatis;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.alibaba.druid.pool.DruidDataSource;
import com.szeastroc.common.jdbc.multi.DynamicDataSource;
import com.szeastroc.common.jdbc.multi.JdbcConfig;
import com.szeastroc.common.jdbc.multi.MasterJdbcConfig;
import com.szeastroc.common.jdbc.page.PageInterceptor;



@Configuration
@EnableTransactionManagement
@MapperScan("com.pengxun.**.dao")
public class MybatisConfig {

	@Bean
	@Qualifier("sqlSessionFactory")
	public SqlSessionFactoryBean sqlSessionFactory(@Autowired DataSource dataSource) throws IOException {
		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
		sqlSessionFactoryBean.setDataSource(dataSource);
		org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
		configuration.setMapUnderscoreToCamelCase(true);
		configuration.addInterceptor(new PageInterceptor());
		sqlSessionFactoryBean.setConfiguration(configuration);
		ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:mapper/**/*.xml"));
		return sqlSessionFactoryBean;
	}
	
	private DataSource buildDataSource(JdbcConfig jdbcConfig) {
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setDriverClassName(jdbcConfig.getDriverClassName());
		dataSource.setUrl(jdbcConfig.getUrl());
		dataSource.setUsername(jdbcConfig.getUsername());
		dataSource.setPassword(jdbcConfig.getPassword());
		dataSource.setMaxActive(32);
		dataSource.setMinIdle(8);
		dataSource.setMaxWait(60000);
		return dataSource;
	}

	@Bean
	public SqlSessionTemplate sqlSessionTemplate(@Autowired SqlSessionFactory sqlSessionFactory) {
		SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
		return sqlSessionTemplate;
	}

	@Bean
	public DataSource basicDataSource(@Autowired MasterShardingJdbcConfig masterJdbcConfig) throws SQLException {
		DataSource dataSource = buildDataSource(masterJdbcConfig);

		DynamicDataSource dynamicDataSource = new DynamicDataSource();
		dynamicDataSource.setDefaultTargetDataSource(dataSource);
		Map<Object, Object> targetDataSources = new HashMap<Object, Object>();
		targetDataSources.put(MasterJdbcConfig.NAME, dataSource);
		dynamicDataSource.setTargetDataSources(targetDataSources);
	    return dynamicDataSource;
	}


}

